package com.bijava.config;

import com.google.common.collect.Sets;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bijava
 * @version 1.0
 * @since 2020-03-11
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
     * web端接口分组
     * @return
     */
    @Bean(value = "createWebApi")
    public Docket createWebApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .protocols(Sets.newHashSet("http","https"))
                .apiInfo(webApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.bijava.controller"))
                .paths(PathSelectors.any())
                .build()
                .groupName("Web端接口服务")
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    /**
     * web端接口描述
     * @return
     */
    private ApiInfo webApiInfo() {
        return new ApiInfoBuilder()
                .title("用户管理系统Web端接口")
                .description("除登录接口以外的所有接口都需要用户令牌，通过按钮Authorize统一授权，token由登录接口获得")
                .contact(new Contact("张三", "https://github.com/zhangsan", "zhangsan@gmail.com"))
                .version("1.0.0")
                .build();
    }

    /**
     * app端接口分组
     * @return
     */
    @Bean(value = "createAppApi")
    public Docket createAppApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(appApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.bijava.controller"))
                .paths(PathSelectors.any())
                .build()
                .groupName("App端接口服务")
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    /**
     * app端接口描述
     * @return
     */
    private ApiInfo appApiInfo() {
        return new ApiInfoBuilder()
                .title("用户管理系统APP端接口")
                .description("除登录接口以外的所有接口都需要用户令牌，通过按钮Authorize统一授权，token由登录接口获得")
                .version("1.0.0")
                .build();
    }

    /**
     * 通过ApiKey设置全局用户令牌
     * @return
     */
    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeys = new ArrayList<>();
        apiKeys.add(new ApiKey("Authorization", "Authorization", "header"));
        return apiKeys;
    }

    /**
     * 设置参数信息
     * @return
     */
    private List<SecurityContext> securityContexts() {
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = new AuthorizationScope("global", "accessEverything");

        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));

        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(SecurityContext.builder()
                .securityReferences(securityReferences)
                .forPaths(PathSelectors.regex("^(?!login).*$"))
                .build());
        return securityContexts;
    }
}
